package com.example.musicplayer.dao

import android.content.ContentValues
import android.provider.BaseColumns
import android.content.Context
import com.example.musicplayer.model.Song
import com.example.musicplayer.util.MusicDatabaseHelper

class MusicDAOImpl(context: Context) : MusicDAO {

    private val dbHelper: MusicDatabaseHelper = MusicDatabaseHelper(context)

    override fun addSong(song: Song) {
        val values = ContentValues().apply {
            put(COLUMN_TITLE, song.title)
            put(COLUMN_ARTIST, song.artist)
            put(COLUMN_IMAGE, song.image)
            put(COLUMN_AUDIO_PATH, song.audioPath)
            put(COLUMN_CATEGORY_ID, song.categoryId)
        }
        dbHelper.writableDatabase.insert(TABLE_SONGS, null, values)
    }

    override fun deleteSong(songId: Int) {
        dbHelper.writableDatabase.delete(TABLE_SONGS, "$COLUMN_ID = ?", arrayOf(songId.toString()))
    }

    fun deleteSongByTitle(title: String): Int {
        return dbHelper.writableDatabase.delete(TABLE_SONGS, "$COLUMN_TITLE = ?", arrayOf(title))
    }

    override fun getSongsByCategoryId(categoryId: Int): List<Song> {
        val songs = mutableListOf<Song>()
        val selection = "$COLUMN_CATEGORY_ID = ?"
        val selectionArgs = arrayOf(categoryId.toString())
        dbHelper.readableDatabase.query(
            TABLE_SONGS,
            null,
            selection,
            selectionArgs,
            null,
            null,
            null
        ).use { cursor ->
            while (cursor.moveToNext()) {
                songs.add(Song(
                    id = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_ID)),
                    title = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_TITLE)),
                    artist = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ARTIST)),
                    image = cursor.getBlob(cursor.getColumnIndexOrThrow(COLUMN_IMAGE)),
                    audioPath = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_AUDIO_PATH)),
                    categoryId = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_CATEGORY_ID))
                ))
            }
        }
        return songs
    }
    fun getAllSongs(): List<Song> {
        val songs = mutableListOf<Song>()
        dbHelper.readableDatabase.query(
            TABLE_SONGS,
            null,
            null,
            null,
            null,
            null,
            null
        ).use { cursor ->
            while (cursor.moveToNext()) {
                songs.add(Song(
                    id = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_ID)),
                    title = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_TITLE)),
                    artist = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ARTIST)),
                    image = cursor.getBlob(cursor.getColumnIndexOrThrow(COLUMN_IMAGE)),
                    audioPath = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_AUDIO_PATH)),
                    categoryId = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_CATEGORY_ID))
                ))
            }
        }
        return songs
    }

    companion object {
        private const val TABLE_SONGS = "songs"
        private const val COLUMN_ID = BaseColumns._ID
        private const val COLUMN_TITLE = "title"
        private const val COLUMN_ARTIST = "artist"
        private const val COLUMN_IMAGE = "image"
        private const val COLUMN_AUDIO_PATH = "audio_path"
        private const val COLUMN_CATEGORY_ID = "category_id"
    }
}